package com.financialDog.javaTool.reportModel;

import com.financialDog.javaTool.common.PropertyConstant;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.HashMap;

public class ReportContext {

    public Integer accountBinarySize;  //指标二进制占位符
    public Integer accountSize;        //指标大小

    public Integer projectBinarySize;  //项目 二进制占位符 （期末期初数）
    public Integer projectSize;        //项目 二进制占位符 （期末期初数）

    public Integer yearBinarySize;     //年份二进制占位符
    public Integer yearSize;           //年份占位符

    public Integer timeBinarySize;     //时间二进制占位符  (月 季度)
    public Integer timeSize;           //时间占位符

    public Integer tableSize;             //表大小 （三大报表）
    public Integer tableBinarySize;       //表的二进制占位符

    public HashMap<String, Integer> balanceAccountMap; //指标

    public HashMap<String, Integer> projectMap; //资产负债表 指标

    public HashMap<String, Integer> timeMap; // 时间

    public HashMap<Integer, BigDecimal> dataMap; //存储数据

    public Integer balanceAccountSize = 0;

    public Integer moneyAccountSize = 0 ;

    public Integer getBalanceAccountPosition(String name){
        if (StringUtils.isEmpty(name)){
            return -1;
        }
        Integer result = balanceAccountMap.get(name);
        if (result == null){
            return -1;
        }
        return result;
    }

    public Integer getProjectPosition(String name){
        if (StringUtils.isEmpty(name)){
            return -1;
        }
        Integer result = projectMap.get(name);
        if (result == null){
            return -1;
        }
        return result;
    }

    public Integer getYearPosition(Integer year){
        if (year < 2000 || year > 2030){
            return -1;
        }
        return year - 2000;
    }


    public Integer getTablePosition(String table){
        if (StringUtils.isEmpty(table)){
            return -1;
        }
        if (table.equals("balanceTable")){
            return 0;
        }
        if (table.equals("profitTable")){
            return 1;
        }
        if (table.equals("moneyTable")){
            return 2;
        }
        return -1;
    }

    public Integer getTimePosition(String time){
        if (StringUtils.isEmpty(time)){
            return -1;
        }
        Integer result = timeMap.get(time);
        if (result == null){
            return -1;
        }
        return result;
    }

    //由于数据量不大，第一个版本不进行数据分区分块进行加速。
    public void initialContext(Integer accountSize, HashMap<String, Integer> balanceAccountMap, Integer balanceAccountSize, Integer moneyAccountSize){

        if (balanceAccountMap.size() == 0){
            return;
        }
        this.balanceAccountMap = balanceAccountMap;

        this.balanceAccountSize = balanceAccountSize;

        this.moneyAccountSize = moneyAccountSize;

        //指标维度
        if (accountSize <= 0 ){
            //指标解析失败, 配置文件出现问题
            return;
        }
        accountBinarySize = Integer.toBinaryString(accountSize).length();

        //期初期末维度
        projectSize = 4;  //期初 期末
        projectBinarySize = 2;

        this.projectMap = new HashMap<String, Integer>();
        this.projectMap.put("start", 0);
        this.projectMap.put("end", 1);

        this.projectMap.put("currentAmount", 2); //本期金额
        this.projectMap.put("yearAmount", 3);    //本年累计金额

        //暂时支持 2000 - 2030年的数据
        yearSize = 30;
        yearBinarySize = Integer.toBinaryString(yearSize).length();

        // 年 1 - 12月  1 -4 季度
        timeSize = 12 + 4 + 1;
        timeBinarySize = Integer.toBinaryString(timeSize).length();
        this.timeMap = new HashMap<String, Integer>();
        timeMap.put("Y", 0);
        timeMap.put("M1",1);
        timeMap.put("M2",2);
        timeMap.put("M3",3);
        timeMap.put("M4",4);
        timeMap.put("M5",5);
        timeMap.put("M6",6);
        timeMap.put("M7",7);
        timeMap.put("M8",8);
        timeMap.put("M9",9);
        timeMap.put("M10",10);
        timeMap.put("M11",11);
        timeMap.put("M12",12);
        timeMap.put("Q1",13);
        timeMap.put("Q2",14);
        timeMap.put("Q3",15);
        timeMap.put("Q4",16);

        // 三大报表
        tableSize = 3;
        tableBinarySize = Integer.toBinaryString(tableSize).length();

        dataMap = new HashMap<Integer, BigDecimal>();
    }

    public void addData(Integer key, BigDecimal data){
        this.dataMap.put(key, data);
    }

    public BigDecimal getData(Integer key){
        return this.dataMap.get(key);
    }

    public PropertyConstant.TABLE_TYPE getTableTypeByAccount(String accountName){
        if (StringUtils.isEmpty(accountName)){
            return PropertyConstant.TABLE_TYPE.ERROR;
        }
        Integer result = balanceAccountMap.get(accountName);
        if(result == null){
            return PropertyConstant.TABLE_TYPE.ERROR;
        }
        if (result < balanceAccountSize){
            return PropertyConstant.TABLE_TYPE.BALANCE_TABLE;
        }else if (result < moneyAccountSize){
            return PropertyConstant.TABLE_TYPE.PROFIT_TABLE;
        }else{
            return PropertyConstant.TABLE_TYPE.MONEY_TABLE;
        }
    }

    public String getCalculateProjectByTableType(PropertyConstant.TABLE_TYPE tableType){
        switch (tableType){
            case PROFIT_TABLE:
            case MONEY_TABLE:
                return "yearAmount";
            case BALANCE_TABLE:
                return "start";
            default:
                return StringUtils.EMPTY;
        }
    }

    public String getTableNameByTableType(PropertyConstant.TABLE_TYPE tableType){
        switch (tableType){
            case PROFIT_TABLE:
                return PropertyConstant.PROFIT_TABLE_NAME;
            case MONEY_TABLE:
                return PropertyConstant.MONEY_TABLE_NAME;
            case BALANCE_TABLE:
                return PropertyConstant.BALANCE_TABLE_NAME;
            default:
                return StringUtils.EMPTY;
        }
    }
}
